package cn.pengge.springaop.reflection;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//获得类的信息
public class Test08 {


    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {

        Class c1 = Class.forName("cn.pengge.springaop.reflection.Person");

        User p = new User();

        c1 = p.getClass();

        System.out.println("======================================");
        //获得类的名字
        System.out.println(c1.getName());  //获得包名 加 类名
        System.out.println(c1.getSimpleName()); //获得类名

        //获得类的属性
        System.out.println("======================================");
        Field[] fields = c1.getFields(); //只能找到 public的属性
//        for (Field field : fields) {
//            System.out.println(field);
//        }
        System.out.println("======================================");

        fields = c1.getDeclaredFields(); //找到全部的属性
        for (Field field : fields) {
            System.out.println(field);
        }

//        Field name = c1.getField("name"); // 只能拿到public 属性
        Field  name = c1.getDeclaredField("name");
        System.out.println("name = " + name);


        //获得类的方法
        Method[] methods = c1.getMethods();  //获得本类 及父类的所有方法
        for (Method method : methods) {
            System.out.println("method = " + method);
        }
        System.out.println("======================================");
        Method[] declaredMethods = c1.getDeclaredMethods(); //只能 获得本类 的所有方法
        for (Method declaredMethod : declaredMethods) {
//            declaredMethod.setAccessible(true);
            System.out.println("declaredMethod = " + declaredMethod);
        }

        //获得指定方法

        //重载
        Method getName = c1.getMethod("getName", null);
        System.out.println("getName = " + getName);

        Method setName = c1.getMethod("setName", String.class);
        System.out.println("setName = " + setName);

        //获得指定的构造器
        System.out.println("======================================");
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("constructor = " + constructor);
        }
        System.out.println("======================================");
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("declaredConstructor = " + declaredConstructor);
        }

        //获得指定的构造器
        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println("declaredConstructor = " + declaredConstructor);

    }

}
